package org.example.myweibo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.myweibo.common.result.JsonResult;
import org.example.myweibo.common.result.StatusCode;
import org.example.myweibo.mapper.WeiboMapper;
import org.example.myweibo.pojo.dto.WeiboDTO;
import org.example.myweibo.pojo.entity.User;
import org.example.myweibo.pojo.entity.Weibo;
import org.example.myweibo.pojo.vo.WeiboIndexVO;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo")
@Slf4j
@Validated
public class WeiboController {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发表微博功能")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {
        log.info("WeiboController.insert(),发表微博");
        log.trace("开始执行insert方法");
        log.debug("weiboDTO:" + weiboDTO);

        User user = (User) session.getAttribute("user");
        if (user == null) {
            log.trace("用户未登录");
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else {
            log.trace("用户已登录");
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO, weibo);
            weibo.setCreated(new Date());
            weibo.setUserId(user.getId());
            weiboMapper.insert(weibo);
            log.trace("插入数据库成功");
            return JsonResult.success(weibo);
        }
    }

    @ApiOperation("获取首页微博列表功能")
    @GetMapping("/selectIndex")
    public JsonResult selectIndexWeibo() throws JsonProcessingException {
        String weiboList = (String) redisTemplate.opsForValue().get("weiboList");
        if (weiboList == null) {
            List<WeiboIndexVO> list = weiboMapper.selectWeibo();
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonString = objectMapper.writeValueAsString(list);
            redisTemplate.opsForValue().set("weiboList", jsonString);
            return JsonResult.success(list);
        }else {
            ObjectMapper objectMapper = new ObjectMapper();
            List<WeiboIndexVO> list = objectMapper.readValue(weiboList, List.class);
            return JsonResult.success(list);
        }
    }

    @ApiOperation("获取微博详情功能")
    @GetMapping("/selectById")
    @ApiImplicitParam(name = "id", value = "微博的ID", required = true, dataType = "int", example = "1")
    public JsonResult selectWeiboDetail(@Range(min = 1,max = 9999, message = "微博id范围必须在1-9999之间") int id) {
        return JsonResult.success(weiboMapper.selectWeiboById(id));
    }
}
